package com.novabluetooth.services.servicedevices;

import com.novabluetooth.services.TJBluetoothLeService;

import android.bluetooth.BluetoothGattService;

/**
 *尿常规 
 * @author nova
 *
 */
public class NiaoJiht111AHanTangService extends TJBluetoothLeService {
	private String NOVAUR_readData = "423404ffff0204";
	private String NOVAUR_closeDevice = "423404FFFF0507";
	String ssString = "";// 血红蛋白数据字符串
	String niaoYeString = "";// 尿液数据字符串
	byte[] niaoyeByte;// 尿液byte数组
	@Override
	public void servicesDiscovered() {
		super.servicesDiscovered();
		BluetoothGattService gattService = mBluetoothGatt.getService(serviceUUID);
		readGattCharact = gattService.getCharacteristic(readCharactUUID);
		writeGattCharact = gattService.getCharacteristic(writeCharactUUID);
		mBluetoothGatt.setCharacteristicNotification(readGattCharact, true);

		if (writeGattCharact == null) {
			System.out.println("writeGattCharact为空");
		}
		ifSuccess = writeDataToDevice(NOVAUR_readData);
		System.out.println("尿液采集数据指令");
	}

	@Override
	public boolean isRightData(byte[] buffer) {
		super.isRightData(buffer);
		niaoYeString += byteArray2HexString(buffer, buffer.length);
		if (niaoYeString.length() > 80) {
			System.out.println("尿液分析仪数据：" + niaoYeString);
			niaoyeByte = String2Byte(niaoYeString);
			ifSuccess = true;
			niaoYeJieXi(niaoyeByte);
		} else {
			ifSuccess = false;
		}
		return ifSuccess;

	}

	@Override
	public void closeDevice() {
		super.closeDevice();
		ifSuccess = writeDataToDevice(NOVAUR_closeDevice);
	}
	public void niaoYeJieXi(byte[] niaoyeByte) {
		// DeviceID
		int idMSB = Integer.parseInt(Integer.toHexString(niaoyeByte[3] & 0xFF), 16);
		int idLSB = Integer.parseInt(Integer.toHexString(niaoyeByte[4] & 0xFF), 16);
		int deviceID = idMSB + idLSB;
		System.out.println("设备编号：" + deviceID);

		// Year
		int yearMSB = Integer.parseInt(Integer.toHexString(niaoyeByte[6] & 0xFF), 16);
		int yearLSB = Integer.parseInt(Integer.toHexString(niaoyeByte[7] & 0xFF), 16);
		int year = yearMSB * 100 + yearLSB;
		System.out.println("年：" + year + "*********");

		// Month
		int month = Integer.parseInt(Integer.toHexString(niaoyeByte[8] & 0xFF), 16);
		System.out.println("月：" + month);

		// Day
		int day = Integer.parseInt(Integer.toHexString(niaoyeByte[9] & 0xFF), 16);
		System.out.println("日：" + day);

		// Hour
		int hour = Integer.parseInt(Integer.toHexString(niaoyeByte[10] & 0xFF), 16);
		System.out.println("时：" + hour);

		// Minutes
		int minute = Integer.parseInt(Integer.toHexString(niaoyeByte[11] & 0xFF), 16);
		System.out.println("分：" + minute);

		// seconds
		int second = Integer.parseInt(Integer.toHexString(niaoyeByte[12] & 0xFF), 16);
		System.out.println("秒：" + second);

		// project1 尿蛋白
		int uro = Integer.parseInt(Integer.toHexString(niaoyeByte[15] & 0xFF), 16);
		System.out.println("uro：" + uro);

		// project2 潜血
		int bld = Integer.parseInt(Integer.toHexString(niaoyeByte[17] & 0xFF), 16);
		System.out.println("bld：" + bld);

		// project3 胆红素
		int bil = Integer.parseInt(Integer.toHexString(niaoyeByte[19] & 0xFF), 16);
		System.out.println("bil：" + bil);

		// project4 胴体
		int ket = Integer.parseInt(Integer.toHexString(niaoyeByte[21] & 0xFF), 16);
		System.out.println("ket：" + ket);

		// project 9 白细胞
		int leu = Integer.parseInt(Integer.toHexString(niaoyeByte[23] & 0xFF), 16);
		System.out.println("leu：" + leu);

		// project 5 葡萄糖
		int glu = Integer.parseInt(Integer.toHexString(niaoyeByte[25] & 0xFF), 16);
		System.out.println("glu：" + glu);

		// project 6 蛋白质
		int pro = Integer.parseInt(Integer.toHexString(niaoyeByte[27] & 0xFF), 16);
		System.out.println("pro：" + pro);

		// project 7 酸碱度
		int ph = Integer.parseInt(Integer.toHexString(niaoyeByte[29] & 0xFF), 16);
		System.out.println("ph：" + ph);

		// project 8 亚硝酸盐
		int nit = Integer.parseInt(Integer.toHexString(niaoyeByte[31] & 0xFF), 16);
		System.out.println("nit：" + nit);

		// project 10 比重
		int sg = Integer.parseInt(Integer.toHexString(niaoyeByte[33] & 0xFF), 16);
		System.out.println("sg：" + sg);

		// project 11 维生素C
		int vc = Integer.parseInt(Integer.toHexString(niaoyeByte[35] & 0xFF), 16);
		System.out.println("vc：" + vc);
		checkData = year + "-" + month + "-" + day + "-" + hour + "-" + minute + "-" + second + "," + uro + "," + bld
				+ "," + bil + "," + ket + "," + glu + "," + pro + "," + ph + "," + nit + "," + leu + "," + sg + ","
				+ vc;
	}
}
